//
//  EmotionModal.swift
//  EmotionInput
//
//  Created by 杨兵 on 15/5/23.
//  Copyright (c) 2015年 bing. All rights reserved.
//  表情模型

import UIKit

private let EmoticonsBundleName = "Emoticons.bundle"

class EmoticonModal: NSObject {
   
    /// 分组名称
    var emoticon_group_name: String
    
    /// 分组路径
    var emoticon_group_path: String
    
    // 健体中文
    var chs: String?
    
    // 图片名称
    var png: String?
    
    // emoji 十六进制编码
    var code: String?
    
    // 表情类型: 0 图片表情 1:emoji
    var type: Int = 0
    
    // emoji字符
    var emojiStr: String?
    
    // 是否是删除按钮
    var isRemoveButton = false
    
    // 表情图片
    var imagePath: String? {
        return emoticon_group_path.stringByAppendingPathComponent(png ?? "")
    }
    
    // 模型初始化
    init(groupName: String, groupPath: String, dict: [String: String]?) {
        emoticon_group_name = groupName
        emoticon_group_path = groupPath
        
        chs = dict?["chs"]
        png = dict?["png"]
        code = dict?["code"]
        
        type = dict?["type"]?.toInt() ?? 0
        if type == 1 {
            // 扫描code
            let scanner = NSScanner(string: code!)
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            
            emojiStr = "\(Character(UnicodeScalar(result)))"
        }
    }
    
    static let emoticonsArray = EmoticonModal.emoticonsList()
    // 加载数组
    private class func emoticonsList() -> [EmoticonModal] {
        var list = [EmoticonModal]()
        
        // 加载path
        let path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: EmoticonsBundleName)
        
        let array = (NSArray(contentsOfFile: path!) as! [[String: String]]).sorted { (dict1, dict2) -> Bool in
            return dict1["emoticon_group_type"] < dict2["emoticon_group_type"]
        }
        
        // 变量数组,获取分组信息
        for group in array {
            let groupname = group["emoticon_group_name"]!
            var grouppath = group["emoticon_group_path"]!
            grouppath = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent(EmoticonsBundleName).stringByAppendingPathComponent(grouppath)
            
            list += groupEmoticons(groupname, grouppath: grouppath)
        }
        
        return list
    }
    
    
    class func groupEmoticons(groupname: String, grouppath: String) -> [EmoticonModal]{
        var list = [EmoticonModal]()
        
        let path = grouppath.stringByAppendingPathComponent("info.plist")
        let dict = NSDictionary(contentsOfFile: path)!
        var array = dict["emoticon_group_emoticons"] as! [[String: String]]
        
        
        
        // 遍历表情数组,将表情数组转换为对象模型
        var count = 0
        for dict in array {
            list.append(EmoticonModal(groupName: groupname, groupPath: grouppath, dict: dict))
            
            if count++ == 19 {
                let emo = EmoticonModal(groupName: groupname, groupPath: grouppath, dict: nil)
                emo.isRemoveButton = true
                list.append(emo)
                count = 0
            }
            
        }
        
        
        // 如果一组不够 21 个表情,就插入空表情
        while (count < 21) && (count != 0) {
            list.append(EmoticonModal(groupName: groupname, groupPath: grouppath, dict: nil))
            count++
        }
        list.last?.isRemoveButton = true
        
//        var i = 0
//        for l in list {
//            println("\(i++) \(l.chs) - \(l.code) - \(l.isRemoveButton)")
//        }
        
        return list
    }
    
    
    
}

















